﻿Imports System.Xml
Imports System.IO

Public Class clsWriteError
    Private Const strNameFileXml As String = "\\ErrorExplain.xml"
    Const strPathFolder As String = "D:\\checkError\\"
    Private strUserName As String
    Private strUserRight As String
    Private strFromName As String
    Private strFunctionName As String
    Private strErrorMessage As String
    Private strErrorException As String
    Private StrDate As String


    Public ReadOnly Property GetstrPathFolder() As String
        Get
            Return strPathFolder
        End Get
    End Property


    Public WriteOnly Property GetstrUserName() As String
        Set(ByVal value As String)
            strUserName = value
        End Set
    End Property

    Public WriteOnly Property GetstrUserRight() As String
        Set(ByVal value As String)
            strUserRight = value
        End Set
    End Property

    Public WriteOnly Property GetstrFromName() As String
        Set(ByVal value As String)
            strFromName = value
        End Set
    End Property

    Public WriteOnly Property GetstrFunctionName() As String
        Set(ByVal value As String)
            strFunctionName = value
        End Set
    End Property

    Public WriteOnly Property GetstrErrorMessage() As String
        Set(ByVal value As String)
            strErrorMessage = value
        End Set
    End Property

    Public WriteOnly Property GetstrErrorException() As String
        Set(ByVal value As String)
            strErrorException = value
        End Set
    End Property

    Public WriteOnly Property Getstrdate() As String
        Set(ByVal value As String)
            StrDate = value
        End Set
    End Property

    Public Sub New()
        strUserName = Nothing
        strUserRight = Nothing
        strFromName = Nothing
        strFunctionName = Nothing
        strErrorMessage = Nothing
        strErrorException = Nothing
        StrDate = Date.Now.ToShortDateString
    End Sub

    Public Sub CreateFileXml(ByVal strBranchCode As String)
        Try
            If Not IO.Directory.Exists(strPathFolder) Then
                IO.Directory.CreateDirectory(strPathFolder)
            End If

            If IO.Directory.Exists(strPathFolder & strBranchCode) Then
                WriteXml(strPathFolder & strBranchCode & strNameFileXml)
            Else
                IO.Directory.CreateDirectory(strPathFolder & strBranchCode)
                WriteXml(strPathFolder & strBranchCode & strNameFileXml)

            End If
        Catch ex As Exception

        End Try

    End Sub

    Public Sub readFolderInDirectory(ByVal ddlNameFile As DropDownList)
        Try
            Dim myDirInfo As DirectoryInfo
            Dim arrFolderInfo As Array
            myDirInfo = New DirectoryInfo(strPathFolder)
            arrFolderInfo = myDirInfo.GetDirectories
            Dim AddItem As ListItem = Nothing

            AddItem = New ListItem("*** กรุณาเลือก ***", 0)
            ddlNameFile.Items.Insert(0, AddItem)

            For iCountArr = 0 To arrFolderInfo.Length - 1
                AddItem = New ListItem(arrFolderInfo(iCountArr).ToString, arrFolderInfo(iCountArr).ToString)
                ddlNameFile.Items.Insert(iCountArr + 1, AddItem)
            Next

            myDirInfo = Nothing
        Catch ex As Exception

        End Try

    End Sub

    Public Sub readFileInDirectory(ByVal ddlNameFile As DropDownList, ByVal strFolderName As String)
        Try
            Dim myDirInfo As DirectoryInfo
            Dim arrFolderInfo As Array
            myDirInfo = New DirectoryInfo(strPathFolder & strFolderName & "\\")
            arrFolderInfo = myDirInfo.GetFiles
            Dim AddItem As ListItem = Nothing

            ddlNameFile.Items.Clear()

            AddItem = New ListItem("*** กรุณาเลือก ***", 0)
            ddlNameFile.Items.Insert(0, AddItem)

            For iCountArr = 0 To arrFolderInfo.Length - 1
                AddItem = New ListItem(arrFolderInfo(iCountArr).ToString, arrFolderInfo(iCountArr).ToString)
                ddlNameFile.Items.Insert(iCountArr + 1, AddItem)
            Next

            myDirInfo = Nothing
        Catch ex As Exception

        End Try

    End Sub
    Private Sub WriteXml(ByVal filePath As String)

        Try
            If (IO.File.Exists(filePath)) Then

                Dim xmldoc As XmlDocument
                xmldoc = New XmlDocument()
                xmldoc.Load(filePath)
                Dim parentNode As XmlElement = xmldoc.CreateElement("ErrorDetail")
                xmldoc.DocumentElement.PrependChild(parentNode)

                Dim a1_UserName As XmlElement = xmldoc.CreateElement("UserName")
                Dim a1_UserRight As XmlElement = xmldoc.CreateElement("UserRight")
                Dim a1_FromName As XmlElement = xmldoc.CreateElement("FromName")
                Dim a1_FunctionName As XmlElement = xmldoc.CreateElement("FunctionName")
                Dim a1_ErrorMessage As XmlElement = xmldoc.CreateElement("ErrorMessage")
                Dim a1_ErrorException As XmlElement = xmldoc.CreateElement("ErrorException")
                Dim a1_Date As XmlElement = xmldoc.CreateElement("date")

                Dim xmla1_UserName As XmlText = xmldoc.CreateTextNode(strUserName)
                Dim xmla1_UserRight As XmlText = xmldoc.CreateTextNode(strUserRight)
                Dim xmla1_FromName As XmlText = xmldoc.CreateTextNode(strFromName)
                Dim xmla1_FunctionName As XmlText = xmldoc.CreateTextNode(strFunctionName)
                Dim xmla1_ErrorMessage As XmlText = xmldoc.CreateTextNode(strErrorMessage)
                Dim xmla1_ErrorException As XmlText = xmldoc.CreateTextNode(strErrorException)
                Dim xmla1_Date As XmlText = xmldoc.CreateTextNode(StrDate)

                parentNode.AppendChild(a1_UserName)
                parentNode.AppendChild(a1_UserRight)
                parentNode.AppendChild(a1_FromName)
                parentNode.AppendChild(a1_FunctionName)
                parentNode.AppendChild(a1_ErrorMessage)
                parentNode.AppendChild(a1_ErrorException)
                parentNode.AppendChild(a1_Date)

                a1_UserName.AppendChild(xmla1_UserName)
                a1_UserRight.AppendChild(xmla1_UserRight)
                a1_FromName.AppendChild(xmla1_FromName)
                a1_FunctionName.AppendChild(xmla1_FunctionName)
                a1_ErrorMessage.AppendChild(xmla1_ErrorMessage)
                a1_ErrorException.AppendChild(xmla1_ErrorException)
                a1_Date.AppendChild(xmla1_Date)


                xmldoc.Save(filePath)
            Else
                Dim xmlDoc As XmlDocument = New XmlDocument()
                Dim XmlDeclaration As XmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "")
                Dim rootNode As XmlElement = xmlDoc.CreateElement("ErrorPRogramKYC")
                xmlDoc.InsertBefore(XmlDeclaration, xmlDoc.DocumentElement)
                xmlDoc.AppendChild(rootNode)
                Dim parentNode As XmlElement = xmlDoc.CreateElement("ErrorDetail")
                xmlDoc.DocumentElement.PrependChild(parentNode)


                Dim a1_UserName As XmlElement = xmlDoc.CreateElement("UserName")
                Dim a1_UserRight As XmlElement = xmlDoc.CreateElement("UserRight")
                Dim a1_FromName As XmlElement = xmlDoc.CreateElement("FromName")
                Dim a1_FunctionName As XmlElement = xmlDoc.CreateElement("FunctionName")
                Dim a1_ErrorMessage As XmlElement = xmlDoc.CreateElement("ErrorMessage")
                Dim a1_ErrorException As XmlElement = xmlDoc.CreateElement("ErrorException")
                Dim a1_Date As XmlElement = xmlDoc.CreateElement("date")

                Dim xmla1_UserName As XmlText = xmlDoc.CreateTextNode(strUserName)
                Dim xmla1_UserRight As XmlText = xmlDoc.CreateTextNode(strUserRight)
                Dim xmla1_FromName As XmlText = xmlDoc.CreateTextNode(strFromName)
                Dim xmla1_FunctionName As XmlText = xmlDoc.CreateTextNode(strFunctionName)
                Dim xmla1_ErrorMessage As XmlText = xmlDoc.CreateTextNode(strErrorMessage)
                Dim xmla1_ErrorException As XmlText = xmlDoc.CreateTextNode(strErrorException)
                Dim xmla1_Date As XmlText = xmlDoc.CreateTextNode(StrDate)

                parentNode.AppendChild(a1_UserName)
                parentNode.AppendChild(a1_UserRight)
                parentNode.AppendChild(a1_FromName)
                parentNode.AppendChild(a1_FunctionName)
                parentNode.AppendChild(a1_ErrorMessage)
                parentNode.AppendChild(a1_ErrorException)
                parentNode.AppendChild(a1_Date)

                a1_UserName.AppendChild(xmla1_UserName)
                a1_UserRight.AppendChild(xmla1_UserRight)
                a1_FromName.AppendChild(xmla1_FromName)
                a1_FunctionName.AppendChild(xmla1_FunctionName)
                a1_ErrorMessage.AppendChild(xmla1_ErrorMessage)
                a1_ErrorException.AppendChild(xmla1_ErrorException)
                a1_Date.AppendChild(xmla1_Date)

                xmlDoc.Save(filePath)
            End If

        Catch ex As Exception

        End Try

    End Sub

End Class
